package com.jucai.pppeiyouclient.comm;

import android.content.Context;

import com.jucai.pppeiyouclient.bean.JsonBean;
import com.jucai.pppeiyouclient.http.HttpUtil;
import com.jucai.pppeiyouclient.http.RequestObject;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.SecurityUtil;
import com.jucai.pppeiyouclient.utils.TextCheckUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by Kuky on 2017-04-14.
 */

public class CommonRequestNetData {

    /**
     * 获取 msg
     *
     * @param context
     * @param mapParam
     * @param jsonStringCallBack
     */
    public static void getJsonStringData(final Context context, Map<String, String> mapParam, boolean show, final JsonStringCallBack jsonStringCallBack,final MinusStringCallBack minusCallBack) {
        RequestObject object = new RequestObject(GlobalAddress.SERVER_ROOT, mapParam);
        HttpUtil.getInstance().doPut(context, object, "正在请求数据，请稍后...", show, new HttpUtil.GetTaskCallBack() {
            @Override
            public void onTaskPostExecute(String callBackStr) {
                try {
                    String content = SecurityUtil.decrypt(callBackStr, "1234567891234567");
                    LogUtils.e("解密之后：" + content);
                    JsonBean jsonBean = JsonBean.getJsonBean(content);
                    if (jsonBean.getRet() == 200) {
                        String msg = jsonBean.getMsg();
                        jsonStringCallBack.parseJsonString(msg);
                    }else if(jsonBean.getRet() == 413){
                        if(minusCallBack != null){
                            minusCallBack.minusJson(jsonBean.getMsg());
                        }
                    }else {
                        LogUtils.e("请求失败：" + jsonBean.getMsg());
                        if (!TextCheckUtils.isEmpty(jsonBean.getMsg())) {
                            ToastUtils.showToast(jsonBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onTaskPostExecuteFailure(String callBackStr) {
                LogUtils.e("请求失败：" + callBackStr);
                if (!TextCheckUtils.isEmpty(callBackStr)) {
                    ToastUtils.showToast(callBackStr);
                }
            }
        });

    }

    /**
     * 获取 msg
     *
     * @param context
     * @param mapParam
     * @param jsonStringCallBack
     */
    public static void getJsonStringData(final Context context, Map<String, String> mapParam, boolean show, final JsonStringCallBack jsonStringCallBack) {
        RequestObject object = new RequestObject(GlobalAddress.SERVER_ROOT, mapParam);
        HttpUtil.getInstance().doPut(context, object, "正在请求数据，请稍后...", show, new HttpUtil.GetTaskCallBack() {
            @Override
            public void onTaskPostExecute(String callBackStr) {
                try {
                    String content = SecurityUtil.decrypt(callBackStr, "1234567891234567");
                    LogUtils.e("解密之后：" + content);
                    JsonBean jsonBean = JsonBean.getJsonBean(content);
                    if (jsonBean.getRet() == 200) {
                        String msg = jsonBean.getMsg();
                        jsonStringCallBack.parseJsonString(msg);
                    } else {
                        LogUtils.e("请求失败：" + jsonBean.getMsg());
                        if (!TextCheckUtils.isEmpty(jsonBean.getMsg())) {
                            ToastUtils.showToast(jsonBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onTaskPostExecuteFailure(String callBackStr) {
                LogUtils.e("请求失败：" + callBackStr);
                if (!TextCheckUtils.isEmpty(callBackStr)) {
                    ToastUtils.showToast(callBackStr);
                }
            }
        });

    }

    public static void getJsonStringDataByEncrpt(final Context context, Map<String, String> mapParam, final ResultCallBack resultCallBack) {
        RequestObject object = new RequestObject(GlobalAddress.SERVER_ROOT, mapParam);
        HttpUtil.getInstance().doGet(context, object, "正在请求数据，请稍后...", false, new HttpUtil.GetTaskCallBack() {
            @Override
            public void onTaskPostExecute(String callBackStr) {
                try {
                    String content = SecurityUtil.decrypt(callBackStr, "1234567891234567");
                    LogUtils.e("解密之后：" + content);
                    JsonBean jsonBean = JsonBean.getJsonBean(content);
                    if (jsonBean.getRet() == 200) {
                        resultCallBack.parseJsonString(jsonBean.getJsonString());
                    } else {
                        resultCallBack.onError(jsonBean.getMsg());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onTaskPostExecuteFailure(String callBackStr) {
                LogUtils.e("请求失败：" + callBackStr);
                if (!TextCheckUtils.isEmpty(callBackStr)) {
                    ToastUtils.showToast(callBackStr);
                }
            }
        });
    }

    /**
     * 获取 data 的 jsonString
     *
     * @param context
     * @param showDialog
     * @param mapParam
     * @param jsonStringCallBack
     */
    public static void getJsonString(final Context context, boolean showDialog, Map<String, String> mapParam, final JsonStringCallBack jsonStringCallBack) {
        RequestObject object = new RequestObject(GlobalAddress.SERVER_ROOT, mapParam);
        HttpUtil.getInstance().doPut(context, object, "正在请求数据，请稍后...", showDialog, new HttpUtil.GetTaskCallBack() {
            @Override
            public void onTaskPostExecute(String callBackStr) {
                try {
                    String content = SecurityUtil.decrypt(callBackStr, "1234567891234567");
                    LogUtils.e("解密之后：" + content);
                    JsonBean jsonBean = JsonBean.getJsonBean(content);
                    if (jsonBean.getRet() == 200) {
                        String jsonString = jsonBean.getJsonString();
                        jsonStringCallBack.parseJsonString(jsonString);
                    } else {
                        LogUtils.e("请求失败：" + jsonBean.getMsg());
                        if (!TextCheckUtils.isEmpty(jsonBean.getMsg())) {
                            ToastUtils.showToast(jsonBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onTaskPostExecuteFailure(String callBackStr) {
                LogUtils.e("请求失败：" + callBackStr);
                if (!TextCheckUtils.isEmpty(callBackStr)) {
                    ToastUtils.showToast(callBackStr);
                }
            }

            ;
        });
    }

    /**
     * 获取 jsonObject
     *
     * @param context
     * @param showDialog
     * @param mapParam
     * @param jsonObjectCallBack
     */
    public static void getJsonObjectData(final Context context, boolean showDialog, Map<String, String> mapParam, final JsonObjectCallBack jsonObjectCallBack) {
        RequestObject object = new RequestObject(GlobalAddress.SERVER_ROOT, mapParam);
        HttpUtil.getInstance().doPut(context, object, "正在请求数据，请稍后...", showDialog, new HttpUtil.GetTaskCallBack() {
            @Override
            public void onTaskPostExecute(String callBackStr) {
                try {
                    String content = SecurityUtil.decrypt(callBackStr, "1234567891234567");
                    LogUtils.e("解密之后：" + content);
                    JsonBean jsonBean = JsonBean.getJsonBean(content, 1);
                    if (jsonBean.getRet() == 200) {
                        JSONObject jsonObject = jsonBean.getJsonObject();
                        jsonObjectCallBack.parseJsonObject(jsonObject);
                    } else {
                        LogUtils.e("请求失败：" + jsonBean.getMsg());
                        if (!TextCheckUtils.isEmpty(jsonBean.getMsg())) {
                            ToastUtils.showToast(jsonBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onTaskPostExecuteFailure(String callBackStr) {
                LogUtils.e("请求失败：" + callBackStr);
                if (!TextCheckUtils.isEmpty(callBackStr)) {
                    ToastUtils.showToast(callBackStr);
                }
            }
        });
    }

    /**
     * 获取 jsonObject
     *
     * @param context
     * @param mapParam
     * @param jsonObjectCallBack
     */
    public static void getJsonObjectData(final Context context, Map<String, String> mapParam, boolean show, final JsonObjectCallBack jsonObjectCallBack) {
        RequestObject object = new RequestObject(GlobalAddress.SERVER_ROOT, mapParam);
        HttpUtil.getInstance().doPut(context, object, "正在请求数据，请稍后...", show, new HttpUtil.GetTaskCallBack() {
            @Override
            public void onTaskPostExecute(String callBackStr) {
                try {
                    String content = SecurityUtil.decrypt(callBackStr, "1234567891234567");
                    LogUtils.e("解密之后：" + content);
                    JsonBean jsonBean = JsonBean.getJsonBean(content, 1);
                    if (jsonBean.getRet() == 200) {
                        JSONObject jsonObject = jsonBean.getJsonObject();
                        jsonObjectCallBack.parseJsonObject(jsonObject);
                    } else {
                        LogUtils.e("请求失败：" + jsonBean.getMsg());
                        if (!TextCheckUtils.isEmpty(jsonBean.getMsg())) {
                            ToastUtils.showToast(jsonBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onTaskPostExecuteFailure(String callBackStr) {
                LogUtils.e("请求失败：" + callBackStr);
                if (!TextCheckUtils.isEmpty(callBackStr)) {
                    ToastUtils.showToast(callBackStr);
                }
            }
        });
    }

    /**
     * 获取 jsonArray
     *
     * @param context
     * @param mapParam
     * @param jsonArrayCallBack
     */
    public static void getJsonArrayData(final Context context, Map<String, String> mapParam, boolean show, final JsonArrayCallBack jsonArrayCallBack) {
        RequestObject object = new RequestObject(GlobalAddress.SERVER_ROOT, mapParam);
        HttpUtil.getInstance().doPut(context, object, "正在请求数据，请稍后...", show, new HttpUtil.GetTaskCallBack() {

            @Override
            public void onTaskPostExecute(String callBackStr) {
                try {
                    String content = SecurityUtil.decrypt(callBackStr, "1234567891234567");
                    LogUtils.e("解密之后：" + content);
                    JsonBean jsonBean = JsonBean.getJsonBean(content, 2);
                    if (jsonBean.getRet() == 200) {
                        JSONArray jsonArray = jsonBean.getJsonArray();
                        jsonArrayCallBack.parseJsonArray(jsonArray);
                    } else {
                        LogUtils.e("请求失败：" + jsonBean.getMsg());
                        if (!TextCheckUtils.isEmpty(jsonBean.getMsg())) {
                            if (!jsonBean.getMsg().equals("订单列表为空")) {
                                ToastUtils.showToast(jsonBean.getMsg());
                            }

                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onTaskPostExecuteFailure(String callBackStr) {
                LogUtils.e("请求失败：" + callBackStr);
                if (!TextCheckUtils.isEmpty(callBackStr)) {
                    ToastUtils.showToast(callBackStr);
                }
            }
        });
    }

    public interface JsonStringCallBack {
        void parseJsonString(String str) throws Exception;
    }

    public interface ResultCallBack {
        void parseJsonString(String str) throws Exception;

        void onError(String str) throws Exception;
    }

    public interface JsonObjectCallBack {
        void parseJsonObject(JSONObject jsonObject) throws Exception;
    }

    public interface JsonArrayCallBack {
        void parseJsonArray(JSONArray jsonArray) throws Exception;
    }

    public interface MinusStringCallBack{
        void minusJson(String string) throws Exception;
    }
}
